<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Video_model extends MY_Model
{

	public function getVideoList($data = array(),$per_page, $limit){
        
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND p.createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND p.createtime <= {$data['endtime']}";
        }
        if(!empty($data['content'])){
            $where .= " AND p.content like '%{$data['content']}%'";
        }
        
        $limit = " limit {$per_page},{$limit}";

        $sql = "SELECT d.*,dep.name department_name FROM tr_video d,tr_department dep WHERE d.isDel<>1  AND dep.id=d.departmentid  {$where}  ORDER BY d.addtime DESC {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }


    public function getVideoCnt($data = array()){
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND p.createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND p.createtime <= {$data['endtime']}";
        }
        if(!empty($data['content'])){
            $where .= " AND p.content like '%{$data['content']}%'";
        }
        if(!empty($data['review_content'])){
            $where .= " AND r.content like '%{$data['review_content']}%'";
        }
        if(!empty($data['fromUsername'])){
            $where .= " AND r.fromUsername = '{$data['fromUsername']}'";
        }
      
        $sql = "SELECT d.*, u.username FROM tr_video d , tr_admin_user u WHERE d.isDel=0 AND d.operationname=u.id  {$where}  ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }


     //编辑
    public function editVideoument($id,$data = array()){
        if($id){
            $this->db->where("videoid",$id);
            $res = $this->db->update("tr_video",$data);
        }else{
            $res = $this->db->insert("tr_video",$data);
        }
        return $res;
    }

    public function delVideoument($id){
        $this->db->where("VideoumentID",$id);
        $data = array(
            'isDel'=>1
        );
        $res = $this->db->update("tr_video",$data);
        return $res;
    }

     //根据id获取广告信息
    public function getVideoInfobyID($id){
        $this->db->where("id",$id);
        $query = $this->db->get("tr_video");
        return $query->row_array();
    }


    public function editStatus($data = array()){
        $this->db->where("id",$data['id']);
        $data = array(
            'isDel'=>1
        );
        $res = $this->db->update("tr_video",$data);
        return $res;
    }
    public function editVideo($Videoumentid = '', $insertData = ''){
        if($Videoumentid){
            $this->db->where("id",$Videoumentid);
            $res = $this->db->update("tr_video",$data);
            //更新
        }else{
            //插入
            if(empty($insertData)){
                return false;
            }
            $sql = "insert into tr_video (title,operationname,fileAttach,addtime,departmentid,fileName,cover) value {$insertData}";
            $res = $this->db->query($sql);
        }
        return $res;
    }

    public function get_cat_list(){
        $this->db->order_by("sort","desc");
        $query = $this->db->get("tr_video");
        return $query->result_array();
    }

}


